package com.ynwt;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.ynwt.domain.Event;
import com.ynwt.domain.ReplyMsg;
import com.ynwt.enums.EventStatus;
import com.ynwt.enums.ReplyMsgType;
import com.ynwt.repository.EventDao;
import com.ynwt.repository.ReplyMsgDao;
import com.ynwt.constant.EventType;
import org.apache.commons.io.FileUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

import java.io.File;
import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;

@SpringBootApplication
public class App implements CommandLineRunner{

	public static void main(String[] args) {
		SpringApplication.run(App.class, args);
	}

	@Override
	public void run(String... strings) {
//		createReplyMsg();
//		try {
//			createEvent();
//		} catch (Exception e) {
//			e.printStackTrace();
//		}
	}

	public void createReplyMsg() {

		try {
//			InputStream in = FileUtils.openInputStream(new File(autoReMsg));
			String json = FileUtils.readFileToString(new File(autoReMsg), "utf-8");
			JSONArray msgs = JSON.parseArray(json);
			System.out.println(msgs);
			for (int i = 0; i < msgs.size(); i++) {
				ReplyMsg replyMsg = new ReplyMsg();
				JSONObject msg = (JSONObject) msgs.get(i);
				replyMsg.setMsgType(ReplyMsgType.TEXT);
//				replyMsg.setId(1 + i);
				replyMsg.setPriority(1);
				replyMsg.setKeyWord(msg.getString("keyWord"));
				replyMsg.setText(msg.getString("text"));
				replyMsg.setUrl(msg.getString("url"));
				replyMsgDao.save(replyMsg);
			}
		} catch (IOException e) {
			System.out.println("读取文件出错");
			e.printStackTrace();
		}
	}

	void createEvent() throws ParseException {
		Event eventReg = new Event(EventType.REGISTER);
		eventReg.setYcoin(20);
		eventReg.setName("注册环球小伙伴送逸贝");
		eventDao.save(eventReg);

		Event eventSub = new Event(EventType.SUBSCRIBE);
		eventSub.setYcoin(20);
		eventSub.setName("关注送逸贝");
		eventDao.save(eventSub);

		Event eventPromoteQrcode = new Event(EventType.QRCODE_PROMOTE);
		eventPromoteQrcode.setYcoin(40);
		eventPromoteQrcode.setName("推广参数二维码");
		eventDao.save(eventPromoteQrcode);

		Event eventHelpQrcode = new Event(EventType.QRCODE_HELP);
		eventHelpQrcode.setYcoin(20);
		eventHelpQrcode.setName("扫描参数二维码关注");
		eventDao.save(eventHelpQrcode);

		Event eventSignOne = new Event(EventType.SIGN_1);
		eventSignOne.setYcoin(3);
		eventSignOne.setName("第一个时间区间签到");
		eventSignOne.setStartTime(new SimpleDateFormat("HH:mm:ss").parse("01:00:00"));
		eventSignOne.setEndTime(new SimpleDateFormat("HH:mm:ss").parse("06:00:00"));
		eventDao.save(eventSignOne);

		Event eventSignTwo = new Event(EventType.SIGN_2);
		eventSignTwo.setYcoin(2);
		eventSignTwo.setName("第二个时间区间签到");
		eventSignTwo.setStartTime(new SimpleDateFormat("HH:mm:ss").parse("06:00:00"));
		eventSignTwo.setEndTime(new SimpleDateFormat("HH:mm:ss").parse("07:30:00"));
		eventDao.save(eventSignTwo);

		Event eventSignThree = new Event(EventType.SIGN_3);
		eventSignThree.setYcoin(1);
		eventSignThree.setName("第三个时间区间签到");
		eventSignThree.setStartTime(new SimpleDateFormat("HH:mm:ss").parse("07:30:00"));
		eventSignThree.setEndTime(new SimpleDateFormat("HH:mm:ss").parse("09:00:00"));
		eventDao.save(eventSignThree);

		Event eventSuccessionSignOne = new Event(EventType.SUCCESSION_SIGN_1);
		eventSuccessionSignOne.setYcoin(1);
		eventSuccessionSignOne.setName("连续签到第一梯度");
		eventSuccessionSignOne.setContent("7");//连续签到的天数
		eventDao.save(eventSuccessionSignOne);

		Event eventSuccessionSignTwo = new Event(EventType.SUCCESSION_SIGN_2);
		eventSuccessionSignTwo.setYcoin(2);
		eventSuccessionSignTwo.setName("连续签到第二梯度");
		eventSuccessionSignTwo.setContent("15");//连续签到的天数
		eventDao.save(eventSuccessionSignTwo);

		Event eventSuccessionSignThree = new Event(EventType.SUCCESSION_SIGN_3);
		eventSuccessionSignThree.setYcoin(3);
		eventSuccessionSignThree.setName("连续签到第三梯度");
		eventSuccessionSignThree.setContent("30");//连续签到的天数
		eventSuccessionSignThree.setStatus(EventStatus.OFF);
		eventDao.save(eventSuccessionSignThree);
	}


	@Value("${autoReMsg}")
	private String autoReMsg;

	@Autowired
	private ReplyMsgDao replyMsgDao;

	@Autowired
	private EventDao eventDao;

}
